<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人网页</title>
    <style>
        body {
            background-color: #fff;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            margin: 0;
            padding: 0;
            text-align: center;
            color: #333;
        }
        h1 {
            margin-top: 50px;
        }
        .graph {
            margin-top: 30px;
            display: inline-block;
            font-size: 0; /* Remove white space between cells */
            touch-action: none; /* Prevent scrolling while dragging on touch devices */
        }
        .graph-row {
            display: block;
            font-size: 0; /* Remove white space between cells */
        }
        .graph-cell {
            width: 30px; /* Increase cell size */
            height: 30px; /* Increase cell size */
            margin: 1px;
            display: inline-block;
            background-color: #ebedf0; /* Light grey color similar to GitHub's contribution graph */
            cursor: pointer; /* Add cursor pointer to indicate clickable */
        }
        .graph-cell.active {
            background-color: #c6e48b; /* Green color similar to GitHub's contribution graph */
        }
        .reset-button {
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 16px;
            background-color: #0366d6; /* GitHub's primary color for buttons */
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        .reset-button:hover {
            background-color: #0550ae; /* Darker shade of GitHub's primary color on hover */
        }
        .copy-button {
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 16px;
            background-color: #0366d6; /* GitHub's primary color for buttons */
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        .copy-button:hover {
            background-color: #0550ae; /* Darker shade of GitHub's primary color on hover */
        }

        .c1 {
            background-color: #c6e48b !important;
        }

        .c2 {
            background-color: #7bc96f !important;
        }

        .c3 {
            background-color: #239a3b !important;
        }

        .c4 {
            background-color: #196127 !important;
        }

    </style>
</head>
<body>
<h1>我的 GitHub 贡献度</h1>
<div class="graph">
    <!-- GitHub 贡献度功能报表 -->
    <script>
        // GitHub 贡献度功能报表的二维数组
        var Led = [
            [0,0,0,0,24,0,0,0,0,24,0,0,0,0,0,24,24,24,24,24,24,24,0,0,0,0,24,24,24,24,24,24,24,0,0,0,24,24,24,24,24,24,0,0,0,24,0,0,24,0,0,0,0,0],
            [0,0,0,24,1,24,24,24,24,1,24,0,0,0,24,16,8,8,1,8,8,16,24,0,0,24,8,8,1,1,1,8,8,24,0,0,24,16,16,16,16,24,0,0,24,24,24,24,24,24,0,0,0,0],
            [0,0,0,24,1,1,1,1,1,1,24,0,0,0,24,8,8,1,1,1,8,8,24,0,24,16,8,8,1,1,1,8,8,16,24,0,24,1,16,16,1,24,0,0,24,1,16,16,1,24,0,0,0,0],
            [0,8,8,24,1,24,1,1,24,1,24,8,8,0,24,16,24,24,24,24,24,16,24,0,24,16,16,24,24,24,24,24,16,16,24,0,24,16,16,16,16,24,0,0,24,24,24,24,24,24,0,0,0,0],
            [0,0,0,24,1,1,16,16,1,1,24,0,0,0,24,24,1,24,1,24,1,24,24,0,0,24,24,1,24,1,24,1,24,24,0,0,24,24,24,24,24,24,0,24,0,0,24,24,0,0,0,0,0,0],
            [0,8,8,24,1,1,1,1,1,1,24,8,8,0,0,24,1,1,1,1,1,24,0,0,0,0,24,1,1,1,1,1,24,0,0,0,0,0,24,24,0,0,0,0,24,24,24,24,24,0,0,0,0,0],
            [0,0,0,0,24,24,24,24,24,24,0,0,0,0,0,0,24,24,24,24,24,0,0,0,0,0,0,24,24,24,24,24,0,0,0,0,24,24,24,24,24,24,0,0,0,0,24,24,24,0,0,0,0,0]
        ];

        // 遍历 Led 数组，在页面中生成对应的方块并设置颜色
        function createGraph() {
            for (var i = 0; i < Led.length; i++) {
                var row = document.createElement("div");
                row.classList.add("graph-row");
                for (var j = 0; j < Led[i].length; j++) {
                    var cell = document.createElement("div");
                    cell.classList.add("graph-cell");
                    if (Led[i][j] !== 0) {
                        cell.classList.add("active");
                    }
                    cell.addEventListener("mousedown", handleMouseDown);
                    cell.addEventListener("mouseenter", handleMouseEnter);
                    cell.addEventListener("mouseup", handleMouseUp);
                    row.appendChild(cell);
                }
                document.querySelector(".graph").appendChild(row);
            }
        }

        // 创建重置按钮并添加点击事件
        function createResetButton() {
            var resetButton = document.createElement("button");
            resetButton.innerText = "重置";
            resetButton.classList.add("reset-button");
            resetButton.addEventListener("click", function() {
                var cells = document.querySelectorAll(".graph-cell.active");
                cells.forEach(function(cell) {
                    cell.classList.remove("active");
                    cell.classList.remove("c1");
                    cell.classList.remove("c2");
                    cell.classList.remove("c3");
                    cell.classList.remove("c4");
                });
                resetLedArray();
            });
            document.body.appendChild(resetButton);
        }

        // 创建复制按钮并添加点击事件
        function createCopyButton() {
            var copyButton = document.createElement("button");
            copyButton.innerText = "复制";
            copyButton.classList.add("copy-button");
            copyButton.addEventListener("click", function() {
                var textarea = document.createElement("textarea");
                textarea.value = JSON.stringify(Led);
                document.body.appendChild(textarea);
                textarea.select();
                document.execCommand('copy');
                document.body.removeChild(textarea);
                alert("Led数组已复制到剪贴板！");
            });
            document.body.appendChild(copyButton);
        }

        // 创建颜色选择下拉菜单并添加事件
        function createColorSelector() {
            var colorSelector = document.createElement("select");
            colorSelector.classList.add("color-selector");
            var colors = ["#c6e48b", "#7bc96f", "#239a3b", "#196127"];
            var colorLabels = ["浅绿", "深绿", "绿", "深深绿"];
            for (var i = 0; i < colors.length; i++) {
                var option = document.createElement("option");
                option.value = colors[i];
                option.text = colorLabels[i];
                colorSelector.appendChild(option);
            }
            colorSelector.addEventListener("change", function() {
                currentColor = this.value;
            });
            console.log(currentColor);
            document.body.appendChild(colorSelector);
        }

        // 重置 Led 数组
        function resetLedArray() {
            for (var i = 0; i < Led.length; i++) {
                for (var j = 0; j < Led[i].length; j++) {
                    Led[i][j] = 0;
                }
            }
        }

        var isDragging = false; // Flag to track if mouse is currently being dragged
        var currentColor = "#c6e48b"; // Default color to set when dragging

        // Function to handle mouse down event on a cell
        function handleMouseDown() {
            isDragging = true;
            toggleColor(this); // Toggle color of the cell when mouse is pressed down
        }

        // Function to handle mouse enter event on a cell (when mouse is dragged)
        function handleMouseEnter() {
            if (isDragging) {
                toggleColor(this); // Toggle color of the cell when mouse is dragged over it
            }
        }

        // Function to handle mouse up event on a cell
        function handleMouseUp() {
            isDragging = false;
        }

        // Function to toggle color of a cell
        function toggleColor(cell) {
            var row = Array.from(cell.parentNode.parentNode.children).indexOf(cell.parentNode);
            var column = Array.from(cell.parentNode.children).indexOf(cell);
            if (cell.classList.contains("active")) {
                cell.classList.remove("active");
                cell.classList.remove("c1");
                cell.classList.remove("c2");
                cell.classList.remove("c3");
                cell.classList.remove("c4");
                Led[row][column] = 0; // Update Led array when cell is deactivated
            } else {
                cell.classList.add("active");
                cell.classList.add(getColorClassName(currentColor));
                Led[row][column] = getColorValue(currentColor); // Update Led array with corresponding color value
            }
        }

        // Function to get color value based on color code
        function getColorValue(color) {
            switch (color) {
                case "#196127":
                    return 24;
                case "#239a3b":
                    return 16;
                case "#7bc96f":
                    return 8;
                case "#c6e48b":
                    return 1;
                default:
                    return 0;
            }
        }

        function getColorClassName(color) {
            switch (color) {
                case "#196127":
                    return "c4";
                case "#239a3b":
                    return "c3";
                case "#7bc96f":
                    return "c2";
                case "#c6e48b":
                    return "c1";
                default:
                    return "c1";
            }
        }

        createGraph(); // 调用函数创建贡献度图表
        createResetButton(); // 调用函数创建重置按钮
        createCopyButton(); // 调用函数创建复制按钮
        createColorSelector(); // 调用函数创建颜色选择器
    </script>
</div>
</body>
</html>
